package com.letoo.dragon.common.utils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created in Intellij IDEA 13 Ultimate
 *
 * @author shit0u
 * @version 2.0
 *          Date: 16/4/28
 *          Time: 09:14
 * @description
 */
public class ListUtils {

    /**
     * 将List按照每批次多少个拆分成多个列表
     *
     * @param rawList     原始列表
     * @param batchAmount 每批次多少个
     * @return
     */
    public static <T> List<List<T>> splitBatchListByThreshold(final List<T> rawList, final int batchAmount) {
        List<List<T>> list = new ArrayList<>();

        int size = rawList.size();

        if (size <= batchAmount) {
            list.add(rawList);
            return list;
        }

        int rest = size % batchAmount;
        int batchs = (size - rest) / batchAmount;

        for (int i = 0; i < batchs; i++) {
            list.add(rawList.subList(i * batchAmount, batchAmount * (i + 1)));
        }

        if (rest > 0) {
            list.add(rawList.subList(batchs * batchAmount, size));
        }

        return list;
    }
}
